package com.heima.controller;

import com.heima.model.UserDao;
import com.heima.domain.User;
import com.heima.domain.User;
import com.heima.model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class UserFindServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        out = response.getWriter();

        if (request.getSession().getAttribute("admin_ok") == null) {

            out.print("您还没有进行管理员登录，请先登录哦！<a href='admin_login.html'>管理员登录</a>");

        } else {

            //1.调用DAO将查询命令推送到服务器上，得到所用用户信息【list集合】
            UserDao dao = new UserDao();
            List<User> userList = dao.findAll();

            //2.调用响应对象，将用户信息结合<table>标签命令以二进制形式写入到响应体
            out.print("<center><h2>已注册用户管理</h2></center>");
            out.print("<table border='1' align='center' cellpadding='5' cellspacing='0'> ");
            out.print("<tr>");
            out.print("<td>用户编号</td>");
            out.print("<td>用户姓名</td>");
            out.print("<td>用户密码</td>");
            out.print("<td>用户性别</td>");
            out.print("<td>用户邮箱</td>");
            out.print("<td>操作</td>");
            out.print("</tr>");

            for (User users : userList) {
                out.print("<tr>");
                out.print("<td>" + users.getUserId() + "</td>");
                out.print("<td>" + users.getUserName() + "</td>");
                out.print("<td> " + users.getPassword() + "</td>");
                out.print("<td>" + users.getSex() + "</td>");
                out.print("<td>" + users.getEmail() + "</td>");
                out.print("<td><a href='/web02/delete?userId=" + users.getUserId() + "'>删除用户</a></td>");
                out.print("</tr>");
            }
            out.print("</table>");
        }
    }
}
